home *** CD-ROM | disk | FTP | other *** search
/ PC for Alla 2003 May / PFA0503.iso / fullversioner / webmenubuilder / WebMenuBuilder2demo / setup.exe / {app} / help / SearchSieve.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-03-22  |  2.8 KB  |  176 lines

  1. public final class SearchSieve {
  2.    private int patternIndex;
  3.    private int textIndex;
  4.    private int mostRecentMatchIndex;
  5.    private String patternString;
  6.    private char[] pattern;
  7.    private int patternLength;
  8.    private int numberOfMatches;
  9.    private boolean exactMatchesOnly;
  10.    private char prevc;
  11.    private char[] buffer;
  12.    private int bufferLength;
  13.    private boolean buffering;
  14.    private char[] swapSpace;
  15.    private static boolean[] wordCharTable = new boolean[256];
  16.  
  17.    public SearchSieve(String var1, boolean var2) {
  18.       this.setPattern(var1, var2);
  19.    }
  20.  
  21.    public final void setPattern(String var1, boolean var2) {
  22.       this.exactMatchesOnly = var2;
  23.       this.patternString = var1;
  24.       this.pattern = var1.toCharArray();
  25.       this.patternLength = this.pattern.length;
  26.       this.buffer = new char[this.patternLength];
  27.       this.swapSpace = new char[this.patternLength];
  28.       this.reset();
  29.    }
  30.  
  31.    public final void reset() {
  32.       this.textIndex = -1;
  33.       this.mostRecentMatchIndex = -1;
  34.       this.patternIndex = 0;
  35.       this.numberOfMatches = 0;
  36.       this.prevc = 0;
  37.       this.bufferLength = 0;
  38.       this.buffering = false;
  39.    }
  40.  
  41.    public final String getPattern() {
  42.       return this.patternString;
  43.    }
  44.  
  45.    public final int getPatternLength() {
  46.       return this.patternLength;
  47.    }
  48.  
  49.    public final int getIndexInText() {
  50.       return this.textIndex;
  51.    }
  52.  
  53.    public final int getMostRecentMatchIndex() {
  54.       return this.mostRecentMatchIndex;
  55.    }
  56.  
  57.    public final int getNumberOfMatches() {
  58.       return this.numberOfMatches;
  59.    }
  60.  
  61.    public final boolean addChar(char var1) {
  62.       boolean var2 = false;
  63.       boolean var3 = false;
  64.       ++this.textIndex;
  65.       if (this.patternLength < 1) {
  66.          return false;
  67.       } else {
  68.          if (this.exactMatchesOnly) {
  69.             if (this.patternIndex == 0) {
  70.                if (var1 == this.pattern[0] && !isWordChar(this.prevc)) {
  71.                   ++this.patternIndex;
  72.                }
  73.             } else if (this.patternIndex != this.patternLength) {
  74.                if (var1 != this.pattern[this.patternIndex]) {
  75.                   this.patternIndex = 0;
  76.                   var3 = true;
  77.                } else {
  78.                   ++this.patternIndex;
  79.                }
  80.             } else {
  81.                if (!isWordChar(var1)) {
  82.                   var2 = true;
  83.                }
  84.  
  85.                this.patternIndex = 0;
  86.                var3 = true;
  87.             }
  88.          } else {
  89.             if (var1 != this.pattern[this.patternIndex]) {
  90.                this.patternIndex = 0;
  91.                var3 = true;
  92.             } else {
  93.                ++this.patternIndex;
  94.             }
  95.  
  96.             if (this.patternIndex == this.patternLength) {
  97.                this.patternIndex = 0;
  98.                var3 = true;
  99.                var2 = true;
  100.             }
  101.          }
  102.  
  103.          if (this.patternIndex > 1 && !this.buffering && var1 == this.pattern[0]) {
  104.             if (!this.exactMatchesOnly || this.exactMatchesOnly && !isWordChar(this.prevc)) {
  105.                this.buffering = true;
  106.                this.buffer[0] = var1;
  107.                this.bufferLength = 1;
  108.             }
  109.          } else if (this.buffering) {
  110.             if (var1 != this.pattern[this.bufferLength]) {
  111.                this.bufferLength = 0;
  112.                this.buffering = false;
  113.             } else {
  114.                this.buffer[this.bufferLength] = var1;
  115.                ++this.bufferLength;
  116.             }
  117.          }
  118.  
  119.          if (var3 && this.bufferLength > 0) {
  120.             this.eatTheBuffer();
  121.          }
  122.  
  123.          this.prevc = var1;
  124.          if (var2) {
  125.             ++this.numberOfMatches;
  126.             this.mostRecentMatchIndex = this.textIndex - this.patternLength + 1;
  127.             if (this.exactMatchesOnly) {
  128.                this.mostRecentMatchIndex += -1;
  129.             }
  130.          }
  131.  
  132.          return var2;
  133.       }
  134.    }
  135.  
  136.    public String toString() {
  137.       return this.getPattern();
  138.    }
  139.  
  140.    private final void eatTheBuffer() {
  141.       this.patternIndex = 0;
  142.       System.arraycopy(this.buffer, 0, this.swapSpace, 0, this.bufferLength);
  143.       int var1 = this.bufferLength;
  144.       this.bufferLength = 0;
  145.       this.buffering = false;
  146.  
  147.       for(int var2 = 0; var2 < var1; ++var2) {
  148.          this.addChar(this.swapSpace[var2]);
  149.       }
  150.  
  151.    }
  152.  
  153.    public static final boolean isWordChar(int var0) {
  154.       return var0 >= 0 && var0 <= 255 ? wordCharTable[var0] : false;
  155.    }
  156.  
  157.    public static final void setWordChar(char var0, boolean var1) {
  158.       if (var0 != 0 && var0 <= 255) {
  159.          wordCharTable[var0] = var1;
  160.       } else {
  161.          throw new IllegalArgumentException("Out of range.");
  162.       }
  163.    }
  164.  
  165.    static {
  166.       for(int var0 = 0; var0 < 256; ++var0) {
  167.          if ((var0 < 65 || var0 > 90) && (var0 < 97 || var0 > 122) && (var0 < 48 || var0 > 57) && var0 < 192) {
  168.             wordCharTable[var0] = false;
  169.          } else {
  170.             wordCharTable[var0] = true;
  171.          }
  172.       }
  173.  
  174.    }
  175. }
  176.